
package net.atremmedia.gwt.actionjava.examples.tween.samples.fountain;

import net.atremmedia.gwt.actionjava.examples.tween.samples.nodes.Particle;
import actionjava.display.Sprite;

public class Fountain extends Sprite
{
	public static final double STAGE_WIDTH = 800;
	public static final double STAGE_HEIGHT = 600;
	public static final double GRAVITY = 0.5;
	public static final int NUM_PARTICLES = 100;
	private Particle[] particles;

	public void start()
	{
		Particle particle;
		particles = new Particle[NUM_PARTICLES];
		for(int i = 0;i < NUM_PARTICLES;i++) {
			particle = new Particle(2, (int) (Math.random() * 0xffffff));
			particle.setX(STAGE_WIDTH / 2);
			particle.setY(STAGE_HEIGHT);
			particle.setVx(Math.random() * 2 - 1);
			particle.setVy(Math.random() * -10 - 10);
			addChild(particle);
			particles[i] = particle;
		}
	}

	public void update()
	{
		Particle particle;
		for(int i = 0;i < NUM_PARTICLES - 1;i++) {
			particle = particles[i];
			particle.plusVy(GRAVITY);
			particle.setX(particle.getX() + particle.getVx());
			particle.setY(particle.getY() + particle.getVy());

			if(particle.getX() - particle.getRadius() > STAGE_WIDTH || particle.getX() + particle.getRadius() < 0 || particle.getY() - particle.getRadius() > STAGE_HEIGHT || particle.getY() + particle.getRadius() < 0) {
				particle.setX(STAGE_WIDTH / 2);
				particle.setY(STAGE_HEIGHT);
				particle.setVx(Math.random() * 2 - 1);
				particle.setVy(Math.random() * -10 - 10);
			}
		}
	}
}
